package com.ts.manage.course.controller;

import com.ts.api.manage.course.CourseControllerApi;
import com.ts.framework.domain.cms.response.CoursePreviewResult;
import com.ts.framework.domain.course.*;
import com.ts.framework.domain.course.ext.CourseView;
import com.ts.framework.domain.course.ext.TeachplanNode;
import com.ts.framework.domain.course.request.CourseListRequest;
import com.ts.framework.domain.course.response.AddCourseResult;
import com.ts.framework.domain.course.response.CoursePublishResult;
import com.ts.framework.model.response.CommonCode;
import com.ts.framework.model.response.QueryResponseResult;
import com.ts.framework.model.response.ResponseResult;
import com.ts.manage.course.service.CourseService;
import com.ts.manage.course.service.TeachPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController implements CourseControllerApi {
    @Autowired
    CourseService courseService;
    @Autowired
    TeachPlanService teachPlanService;

    /**
     * 查询课程计划
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachPlanByCourseId(@PathVariable("courseId") String courseId) {
        return teachPlanService.findTeachPlanByCourseId(courseId);
    }

    /**
     * 添加课程计划
     * @param teachplan
     * @return
     */
    @Override
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        return teachPlanService.addTeachPlan(teachplan);
    }

    /**
     * 删除课程计划
     * @param courseId
     * @return
     */
    @Override
    @DeleteMapping("teachplan/del/{courseid}")
    public ResponseResult delTeachplan(@PathVariable("courseid") String courseId) {
        return teachPlanService.delTeachplan(courseId);
    }

    /**
     * 分页查询课程列表
     * @param page
     * @param size
     * @param courseListRequest
     * @return
     */
    @Override
    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult findCourseListByPage(@PathVariable("page") int page,
                                                    @PathVariable("size") int size,
                                                    CourseListRequest courseListRequest) {
        return courseService.findCourseListByPage(page, size, courseListRequest);
    }

    /**
     * 添加课程信息
     * @param courseBase
     * @return
     */
    @Override
    @PostMapping("/coursebase/add")
    public AddCourseResult addCourseBase(@RequestBody CourseBase courseBase) {
        return courseService.addCourseBase(courseBase);
    }

    /**
     * 查询课程基础信息
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("/coursebase/get/{courseId}")
    public CourseBase findCourseBaseById(@PathVariable("courseId") String courseId) {
        return courseService.findCourseBaseById(courseId);
    }

    /**
     * 更新课程基础信息
     * @param id
     * @param courseBase
     * @return
     */
    @Override
    @PutMapping("/coursebase/update/{id}")
    public ResponseResult updateCourseBase(@PathVariable("id") String id,
                                           @RequestBody CourseBase courseBase) {
        return courseService.updateCourseBase(id, courseBase);
    }

    /**
     * 获取课程营销信息
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("coursemarket/get/{courseId}")
    public CourseMarket getCourseMarketById(@PathVariable("courseId") String courseId) {
        return courseService.getCourseMarketById(courseId);
    }

    /**
     * 更新课程营销信息
     * @param id
     * @param courseMarket
     * @return
     */
    @Override
    @PutMapping("coursemarket/update/{id}")
    public ResponseResult updateCourseMarket(@PathVariable("id") String id, @RequestBody CourseMarket courseMarket) {
        CourseMarket courseMarketDB = courseService.updateCourseMarket(id, courseMarket);
        if (courseMarketDB != null) {
            return new ResponseResult(CommonCode.SUCCESS);
        } else {
            return new ResponseResult(CommonCode.FAIL);
        }
    }

    /**
     * 添加课程图片
     * @param courseId
     * @param pic
     * @return
     */
    @Override
    @PostMapping("/coursepic/add")
    public ResponseResult addCoursePic(@RequestParam("courseId") String courseId, @RequestParam("pic") String pic) {
        return courseService.addCoursePic(courseId,pic);
    }

    /**
     * 获取课程图片
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("/coursepic/list/{courseId}")
    public CoursePic findCoursePic(@PathVariable("courseId") String courseId) {
        return courseService.findCoursePic(courseId);
    }

    /**
     * 删除课程图片
     * @param courseId
     * @return
     */
    @Override
    @DeleteMapping("/coursepic/delete")
    public ResponseResult delCoursePic(@RequestParam("courseId") String courseId) {
        return courseService.delCoursePic(courseId);
    }

    /**
     * 查询课程详情页面模型数据
     * @param id
     * @return
     */
    @Override
    @GetMapping("/courseview/{courseId}")
    public CourseView getCourseView(@PathVariable("courseId") String courseId) {
        return courseService.getCourseView(courseId);
    }

    /**
     * 课程详情预览url
     * @param id
     * @return
     */
    @Override
    @PostMapping("/preview/{id}")
    public CoursePreviewResult coursePreview(@PathVariable("id") String id) {
        return courseService.coursePreview(id);
    }

    /**
     * 课程发布
     * @param id
     * @return
     */
    @Override
    @PostMapping("/publish/{id}")
    public CoursePublishResult coursePublish(@PathVariable("id") String id) {
        return courseService.coursePublish(id);
    }

    /**
     * 保存课程计划与视频的关联关系
     * @param teachplanMedia
     * @return
     */
    @Override
    @PostMapping("/savemedia")
    public ResponseResult saveTeachMedia(@RequestBody TeachplanMedia teachplanMedia) {
        return teachPlanService.saveTeachplanMedia(teachplanMedia);
    }


}
